//
//  RichButton.m
//  PollutantSource
//
//  Created by Potter on 15-1-10.
//  Copyright (c) 2015年 etouch. All rights reserved.
//

#import "RichButton.h"

@interface RichButton()
{
    UIColor * _backgroundColorNormal;
    UIColor * _backgroundColorHighlighted;
    UIColor * _backgroundColorDisabled;
    UIColor * _backgroundColorSelected;
    UIEdgeInsets _titleEdgeInsets;
}

@end

@implementation RichButton
#define INTERVAL 5

#pragma mark - overide Super Method

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initDefault];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self initDefault];
    
}

- (void)initDefault
{
    if (!_interval) {
        _interval = [NSNumber numberWithFloat:INTERVAL];
    }
    self.richFont = self.titleLabel.font;
    self.style = RichButtonImgLeft;
}

-(void)setRichFont:(UIFont *)richFont{
    _richFont = richFont;
    self.titleLabel.font = richFont;
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat textW = 0.0f;
    if([[UIDevice currentDevice].systemVersion floatValue] >= 7.0){
        textW = [self.currentTitle sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:self.richFont,NSFontAttributeName, nil]].width;
    } else {
        textW = [self.currentTitle sizeWithAttributes:@{NSFontAttributeName:self.richFont}].width;
    }
    CGFloat iconW = self.imageView.image.size.width;
    CGRect frame = contentRect;
    
    switch (self.style) {
        case RichButtonImgLeft:
        {
            if (iconW == 0) {
                frame.origin.x = (self.frame.size.width - textW)/2;
            } else {
                frame.origin.x = (self.frame.size.width - textW - iconW - _interval.floatValue)/2 + iconW + _interval.floatValue;
            }
        }
            break;
        case RichButtonImgRight:
        {
            if (iconW == 0) {
                frame.origin.x = (self.frame.size.width - textW)/2;
            } else {
                frame.origin.x = (self.frame.size.width - textW - iconW - _interval.floatValue)/2;
            }
        }
            break;
        case RichButtonImgLeftAlignmentLeft:
        {
            if (iconW == 0) {
                frame.origin.x = 0;
            } else {
                frame.origin.x = iconW+_interval.floatValue;
            }
        }
            break;
        case RichButtonImgLeftAlignmentRight:
        {
            frame.origin.x = self.frame.size.width-textW;
        }
            break;
        case RichButtonImgRightAlignmentLeft:
        {
            frame.origin.x = 0;
        }
            break;
        case RichButtonImgRightAlignmentRight:
        {
            if (iconW == 0) {
                frame.origin.x = self.frame.size.width-textW;
            } else {
                frame.origin.x = self.frame.size.width - iconW -_interval.floatValue - textW;
            }
        }
            break;
        default:
            break;
    }
    
    frame.origin.y = _titleEdgeInsets.bottom;
    frame.size.width = textW;
    frame.size.height = self.bounds.size.height;
    return frame;
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat textW = 0.0f;
    if([[UIDevice currentDevice].systemVersion floatValue] >= 7.0){
        textW = [self.currentTitle sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:self.richFont,NSFontAttributeName, nil]].width;
    } else {
        textW = [self.currentTitle sizeWithAttributes:@{NSFontAttributeName:self.richFont}].width;
    }
    CGFloat iconW = self.currentImage.size.width;
    CGFloat iconH = self.currentImage.size.height;
    CGFloat iconX = 0;
    
    switch (self.style) {
        case RichButtonImgLeft:
        {
            if (textW == 0) {
                iconX = (self.frame.size.width - iconW)/2;
            } else {
                iconX = (self.frame.size.width - textW - iconW - _interval.floatValue)/2;
            }
        }
            break;
        case RichButtonImgRight:
        {
            if (textW == 0) {
                iconX = (self.frame.size.width - iconW)/2;
            } else {
                iconX = (self.frame.size.width - textW - iconW - _interval.floatValue)/2 + textW + _interval.floatValue;
            }
        }
            break;
        case RichButtonImgLeftAlignmentLeft:
        {
            iconX = 0;
        }
            break;
        case RichButtonImgLeftAlignmentRight:
        {
            if (textW == 0) {
                iconX = self.frame.size.width - textW - iconW - _interval.floatValue;
            } else {
                iconX = self.frame.size.width - iconW;
            }
        }
            break;
        case RichButtonImgRightAlignmentLeft:
        {
            if (textW == 0) {
                iconX = 0;
            } else {
                iconX = textW+_interval.floatValue;
            }
        }
            break;
        case RichButtonImgRightAlignmentRight:
        {
            iconX = self.frame.size.width-iconW;
        }
            break;
        default:
            break;
    }
    
    CGFloat iconY = (self.frame.size.height - iconH)/2;
    CGRect frame = contentRect;
    frame.size.width = iconW;
    frame.size.height = iconH;
    frame.origin.x = iconX;
    frame.origin.y = iconY;
    return frame;
}

- (void)setImage:(UIImage *)image forState:(UIControlState)state
{
    [super setImage:image forState:state];
    if (!image) {
        self.imageView.image = nil;
    }
}

- (void)setTitleEdgeInsets:(UIEdgeInsets)titleEdgeInsets
{
    [super setTitleEdgeInsets:titleEdgeInsets];
    _titleEdgeInsets = titleEdgeInsets;
}

- (void)setHighlighted:(BOOL)highlighted
{
    if (highlighted) {
        if(_backgroundColorHighlighted){
            self.backgroundColor = _backgroundColorHighlighted;
        }
    }else
    {
        if(_backgroundColorNormal){
            self.backgroundColor = _backgroundColorNormal;
        }
    }
}

#pragma mark - public Method

- (void)setStyle:(RichButtonStyle)style
{
    _style = style;
}

- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state
{
    if (backgroundColor != nil) {
        switch (state) {
            case UIControlStateNormal:
            {
                _backgroundColorNormal = backgroundColor;
                self.backgroundColor = _backgroundColorNormal;
            }
                break;
            case UIControlStateHighlighted:
            {
                _backgroundColorHighlighted = backgroundColor;
            }
                break;
            case UIControlStateDisabled:
            {
                _backgroundColorDisabled = backgroundColor;
            }
                break;
            case UIControlStateSelected:
            {
                _backgroundColorSelected = backgroundColor;
            }
                break;
            default:
                break;
        }
    }
}


@end
